// 联盟
(function(){
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.league;
	var db_id_prefix = config.dbIdPrefix.league;
	var BusCon = require("../bus/_con");
	var helper = require("../_include/helper");

	//构造函数
	var _con = function (league_name,info){
		var self = this;
		if (league_name = league_name.trim()) {
			throwCusError("联盟名称不可为空")
		}
		//ID编号
		self._id = league_name||(db_id_prefix+Math.random().toString(16).substr(2)+(+new Date).toString(16));
		//校验联盟属性
		//联盟名称
		if (!helper.folder_name(league_name)) {
			throwCusError("联盟名命名不规范")
		}
		if (db.find_by_id(db_table_name,league_name)) {
			throwCusError("已经存在名字相同的联盟名称")
		}
		self.league_name = league_name;
		//联盟介绍
		self.intro = info.intro?String(info.intro):"";
		if (self.intro.length>config.leagueDescriptionLength) {
			throwCusError("联盟简介长度超出长度限制");
		}
		//活动
		self.activitys = [];
		// //可做销售的商品
		// self.marketGoods = [];
		//销售
		self.markets = [];
		//联盟商家
		self.merchants = [];
		//联盟管理员
		//格式为：{bus_id:["all"],}
		self.admins = [];
		//联盟创建者
		// self.avalon = info.avalon;
		self._setAvalon(info.avalon);
		//创建时间
		self.create_time = +new Date;
		//状态：未审核，审核中，审核通过、审核未通过、冻结（被举报并查出有问题）
		self.status = "未审核";

		db.insert(db_table_name,self);
		console.log("创建了一个联盟：",league_name);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};
}());
